using System;
using MSSystem = System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.IO;
using System.Text;
using System.Net.Mail;
using Toenda.Rails.Data;
using Toenda.Rails.DataContainer;
using Toenda.TFL.Base;
using Toenda.TFL.Data;

namespace Toenda.Rails {
	namespace Data {
		public class DBImage : DBClient {
			// ---------------------------------------------------------
			// CONSTRUCTORS
			// ---------------------------------------------------------

			/// <summary>
			/// Default constructor
			/// </summary>
			public DBImage() {
			}

			// ---------------------------------------------------------
			// PUBLIC MEMBERS
			// ---------------------------------------------------------

			/// <summary>
			/// Get the amount of all images of a user.
			/// </summary>
			/// <param name="UserID">The ID of a user.</param>
			/// <param name="word"></param>
			/// <exception cref="ArgumentNullException">If UserID is null.</exception>
			/// <returns>The amount of all images of a user</returns>
			public int GetImageAmount(string UserID, int type, string word) {
				if(UserID == null) {
					throw new ArgumentNullException("UserID");
				}

				if(word == null) {
					throw new ArgumentNullException("word");
				}

				StringBuilder str = new StringBuilder();

				str.Append("SELECT COUNT(pkid) AS amount");
				str.Append(" FROM [dbo].[tbl_images]");
				str.Append(" WHERE access = 0");
				str.Append(" AND user_pkid = '" + UserID + "'");

				if(type > 0) {
					str.Append(" AND (type = " + type.ToString() + ") ");
				}

				str.Append(" AND (name LIKE '%" + word + "%' ");
				str.Append(" OR description LIKE '%" + word + "%') ");

				DataSet data = base.DBBaseClass.ExecuteQuery(str.ToString());
				DataTableReader reader = data.CreateDataReader();
				reader.Read();

				return reader.GetInt32(reader.GetOrdinal("amount"));
			}

			/// <summary>
			/// Get the amount of all images of a user.
			/// </summary>
			/// <param name="UserID">The ID of a user.</param>
			/// <exception cref="ArgumentNullException">If UserID is null.</exception>
			/// <returns>The amount of all images of a user</returns>
			public int GetImageAmount(string UserID) {
				if(UserID == null) {
					throw new ArgumentNullException("UserID");
				}

				return this.GetImageAmount(UserID, 0, false);
			}

			/// <summary>
			/// Get the amount of all images
			/// </summary>
			/// <returns>The amount of all images</returns>
			public int GetImageAmount() {
				return this.GetImageAmount("", 0, true);
			}

			/// <summary>
			/// Get the amount of all images of a user.
			/// </summary>
			/// <param name="word">The ID of a user or the search word.</param>
			/// <param name="onlypublic"></param>
			/// <exception cref="ArgumentNullException">If word is null.</exception>
			/// <returns>The amount of all images of a user</returns>
			public int GetImageAmount(string word, int type, bool onlypublic) {
				if(word == null) {
					throw new ArgumentNullException("word");
				}

				StringBuilder str = new StringBuilder();

				str.Append("SELECT COUNT(pkid) AS amount");
				str.Append(" FROM [dbo].[tbl_images]");
				str.Append(" WHERE access = 0");

				if(onlypublic && word != "") {
					str.Append(" AND (name LIKE '%" + word + "%' ");
					str.Append(" OR description LIKE '%" + word + "%') ");
				}
				else if(!onlypublic) {
					str.Append(" AND user_pkid = '" + word + "'");
				}

				if(type > 0) {
					str.Append(" AND (type = " + type.ToString() + ") ");
				}

				DataSet data = base.DBBaseClass.ExecuteQuery(str.ToString());
				DataTableReader reader = data.CreateDataReader();
				reader.Read();

				return reader.GetInt32(reader.GetOrdinal("amount"));
			}

			/// <summary>
			/// Get the amount of all images of a user.
			/// </summary>
			/// <param name="word">The ID of a user or the search word.</param>
			/// <param name="onlypublic"></param>
			/// <exception cref="ArgumentNullException">If word is null.</exception>
			/// <returns>The amount of all images of a user</returns>
			public int GetImageAmount(string userID, bool isLoggedIn) {
				if(userID == null) {
					throw new ArgumentNullException("userID");
				}

				StringBuilder str = new StringBuilder();

				str.Append("SELECT COUNT(pkid) AS amount");
				str.Append(" FROM [dbo].[tbl_images]");

				if(isLoggedIn) {
					str.Append(" WHERE (NOT access IS NULL) ");
				}
				else {
					str.Append(" WHERE access = 0 ");
				}

				str.Append(" AND user_pkid = '" + userID + "'");

				DataSet data = base.DBBaseClass.ExecuteQuery(str.ToString());
				DataTableReader reader = data.CreateDataReader();
				reader.Read();

				return reader.GetInt32(reader.GetOrdinal("amount"));
			}

			/// <summary>
			/// Get the amount of all favorite images of a user.
			/// </summary>
			/// <param name="word">The ID of a user or the search word.</param>
			/// <param name="onlypublic"></param>
			/// <exception cref="ArgumentNullException">If word is null.</exception>
			/// <returns>The amount of all images of a user</returns>
			public int GetFavoriteImageAmount(string userid) {
				if(userid == null) {
					throw new ArgumentNullException("userid");
				}

				StringBuilder str = new StringBuilder();

				str.Append("SELECT COUNT(pkid) AS amount");
				str.Append(" FROM [dbo].[tbl_favorites]");
				str.Append(" WHERE user_pkid = '" + userid + "'");

				DataSet data = base.DBBaseClass.ExecuteQuery(str.ToString());
				DataTableReader reader = data.CreateDataReader();
				reader.Read();

				return reader.GetInt32(reader.GetOrdinal("amount"));
			}

			/// <summary>
			/// Get the amount of all images of a tag.
			/// </summary>
			/// <param name="word">The ID of a user or the search tag.</param>
			/// <exception cref="ArgumentNullException">If tag is null.</exception>
			/// <returns>The amount of all images of a tag</returns>
			public int GetImageAmountByTag(string tag, int type) {
				return this.GetImageAmountByTag("", tag, type);
			}

			/// <summary>
			/// Get the amount of all images of a tag.
			/// </summary>
			/// <param name="tag">The search tag.</param>
			/// <param name="userid">The user id</param>
			/// <exception cref="ArgumentNullException">If tag is null.</exception>
			/// <returns>The amount of all images of a tag</returns>
			public int GetImageAmountByTag(string userid, string tag, int type) {
				if(userid == null) {
					throw new ArgumentNullException("userid");
				}

				if(tag == null) {
					throw new ArgumentNullException("tag");
				}

				StringBuilder str = new StringBuilder();

				str.Append("SELECT DISTINCT COUNT(img.pkid) AS amount ");
				str.Append("FROM tbl_tags AS tags ");
				str.Append("INNER JOIN tbl_images_to_tags AS itt ");
				str.Append("ON itt.tag_pkid = tags.pkid ");
				str.Append("INNER JOIN tbl_images AS img ");
				str.Append("ON itt.image_pkid = img.pkid ");

				if(userid.Trim() != "") {
					str.Append("INNER JOIN tbl_user AS usr ");
					str.Append("ON img.user_pkid = usr.pkid ");
				}

				str.Append("WHERE tag = '" + tag + "' ");

				if(userid.Trim() != "") {
					str.Append("AND usr.[pkid] = '" + userid + "' ");
				}
				else {
					str.Append(" AND img.access = 0 ");
				}

				if(type > 0) {
					str.Append(" AND (img.type = " + type.ToString() + ") ");
				}

				DataSet data = base.DBBaseClass.ExecuteQuery(str.ToString());
				DataTableReader reader = data.CreateDataReader();
				reader.Read();

				return reader.GetInt32(reader.GetOrdinal("amount"));
			}

			/// <summary>
			/// Get the amount of all images of a cam.
			/// </summary>
			/// <param name="cam">The search cam.</param>
			/// <param name="type"></param>
			/// <exception cref="ArgumentNullException">If cam is null.</exception>
			/// <returns>The amount of all images of a cam</returns>
			public int GetImageAmountByCam(string cam, int type, bool isLoggedIn) {
				return this.GetImageAmountByCam("", cam, type, isLoggedIn);
			}

			/// <summary>
			/// Get the amount of all images of a cam.
			/// </summary>
			/// <param name="userID">The user id</param>
			/// <param name="cam">The search cam.</param>
			/// <param name="isLoggedIn"></param>
			/// <param name="type"></param>
			/// <exception cref="ArgumentNullException">If cam is null.</exception>
			/// <returns>The amount of all images of a tag</returns>
			public int GetImageAmountByCam(string userID, string cam, int type, bool isLoggedIn) {
				if(userID == null) {
					throw new ArgumentNullException("userID");
				}

				if(cam == null) {
					throw new ArgumentNullException("cam");
				}

				StringBuilder str = new StringBuilder();

				str.Append("SELECT SUM(equipment_model_amount) AS equipment_model_amount FROM ( ");
				str.Append(" SELECT ");
				str.Append(" COUNT(equipment_model) AS equipment_model_amount ");
				str.Append(" FROM tbl_images ");
				str.Append(" WHERE NOT (equipment_model IS NULL) ");
				str.Append(" AND NOT (equipment_maker IS NULL) ");

				str.Append(" AND ( ");
				str.Append(" equipment_maker LIKE '%" + cam + "%' ");
				str.Append(" OR equipment_model LIKE '%" + cam + "%' ");
				str.Append(" OR equipment_maker + ' ' + equipment_model LIKE '%" + cam + "%' ");
				str.Append(" ) ");

				if(isLoggedIn && userID != "") {
					str.Append(" AND (NOT access IS NULL) ");
					str.Append(" AND (pkid = '" + userID + "') ");
				}
				else if(isLoggedIn) {
					str.Append(" AND (NOT access IS NULL) ");
				}
				else {
					str.Append(" AND access = 0 ");
				}

				if(type > 0) {
					str.Append(" AND (type = " + type.ToString() + ") ");
				}

				str.Append(" ) AS tmp ");

				DataSet data = base.DBBaseClass.ExecuteQuery(str.ToString());

				if(data.Tables.Count > 0) {
					DataTableReader reader = data.CreateDataReader();
					reader.Read();
					
					try {
					    return reader.GetInt32(reader.GetOrdinal("equipment_model_amount"));
					}
					catch(Exception ex) {
						string s = ex.Message;
					    return 0;
					}
				}
				else {
					return 0;
				}
			}

			/// <summary>
			/// Get the amount of all sets of a user.
			/// </summary>
			/// <param name="word">The ID of a user.</param>
			/// <exception cref="ArgumentNullException">If id or userid is null.</exception>
			/// <returns>The amount of all sets of a user</returns>
			public int GetSetAmount(string id, string userid) {
				return this.GetSetAmount(id, userid, false);
			}

			/// <summary>
			/// Get the amount of all sets of a user.
			/// </summary>
			/// <param name="word">The ID of a user.</param>
			/// <exception cref="ArgumentNullException">If id or userid is null.</exception>
			/// <returns>The amount of all sets of a user</returns>
			public int GetSetAmount(string id, string userid, bool isloggedin) {
				if(id == null) {
					throw new ArgumentNullException("id");
				}

				if(userid == null) {
					throw new ArgumentNullException("userid");
				}

				StringBuilder str = new StringBuilder();

				str.Append("SELECT COUNT(sti.pkid) AS amount ");
				str.Append("FROM [dbo].[tbl_sets_to_images] AS sti ");
				str.Append("INNER JOIN [dbo].[tbl_sets] AS sets ");
				str.Append("ON sets.pkid = sti.set_pkid ");
				str.Append("INNER JOIN [dbo].[tbl_images] AS img ");
				str.Append("ON img.pkid = sti.image_pkid ");
				str.Append("WHERE sets.pkid = '" + id + "'");
				str.Append("AND sets.user_pkid = '" + userid + "'");

				if(!isloggedin) {
					str.Append("AND img.access = 0");
				}

				DataSet data = base.DBBaseClass.ExecuteQuery(str.ToString());
				DataTableReader reader = data.CreateDataReader();
				reader.Read();

				return reader.GetInt32(reader.GetOrdinal("amount"));
			}

			/// <summary>
			/// Get the amount of all comments of a image.
			/// </summary>
			/// <param name="ImageID">The ID of a image (guid).</param>
			/// <exception cref="ArgumentNullException">If ImageID is null.</exception>
			/// <returns>The amount of all comments of a image</returns>
			public int GetImageCommentAmount(string ImageID) {
				if(ImageID == null)
					throw new ArgumentNullException("ImageID");

				StringBuilder str = new StringBuilder();

				str.Append("SELECT COUNT(pkid) AS amount");
				str.Append(" FROM [dbo].[tbl_comments]");
				str.Append(" WHERE image_pkid = '" + ImageID + "'");

				DataSet data = base.DBBaseClass.ExecuteQuery(str.ToString());
				DataTableReader reader = data.CreateDataReader();
				reader.Read();

				return reader.GetInt32(reader.GetOrdinal("amount"));
			}

			/// <summary>
			/// Get the amount of all views of a image.
			/// </summary>
			/// <param name="ImageID">The ID of a image.</param>
			/// <exception cref="ArgumentNullException">If ImageID is null.</exception>
			/// <returns>The amount of all views of a image</returns>
			public long GetImageViewAmount(string ImageID) {
				if(ImageID == null)
					throw new ArgumentNullException("ImageID");

				StringBuilder str = new StringBuilder();

				str.Append("SELECT [view]");
				str.Append(" FROM [dbo].[tbl_image_view]");
				str.Append(" WHERE image_pkid = '" + ImageID + "'");

				DataSet data = base.DBBaseClass.ExecuteQuery(str.ToString());

				if(data.Tables[0].Rows.Count > 0) {
					DataTableReader reader = data.CreateDataReader();
					reader.Read();

					return reader.GetInt64(reader.GetOrdinal("view"));
				}
				else
					return 0;
			}

			/// <summary>
			/// Get the amount of all views from a user.
			/// </summary>
			/// <param name="UserID">The ID of a user.</param>
			/// <exception cref="ArgumentNullException">If UserID is null.</exception>
			/// <returns>The amount of all views from a user.</returns>
			public long GetUserViewAmount(string UserID) {
				if(UserID == null)
					throw new ArgumentNullException("UserID");

				StringBuilder str = new StringBuilder();

				str.Append("SELECT SUM(v.[view]) AS [views]");
				str.Append(" FROM [dbo].[tbl_image_view] AS v");
				str.Append(" INNER JOIN [dbo].[tbl_images] AS i");
				str.Append(" ON i.pkid = v.image_pkid");
				str.Append(" WHERE i.user_pkid = '" + UserID + "'");

				DataSet data = base.DBBaseClass.ExecuteQuery(str.ToString());

				if(data.Tables[0].Rows.Count > 0) {
					DataTableReader reader = data.CreateDataReader();
					reader.Read();

					if(!reader.IsDBNull(reader.GetOrdinal("views"))) {
						return reader.GetInt64(reader.GetOrdinal("views"));
					}
					else {
						return 0;
					}
				}
				else
					return 0;
			}

			/// <summary>
			/// Get a image item
			/// </summary>
			/// <param name="id">The id of a image as string.</param>
			/// <exception cref="ArgumentNullException">If id is null.</exception>
			/// <returns>A image</returns>
			public ImageItem GetImageByImageID(string id) {
				if(id == null)
					throw new ArgumentNullException("id");

				StringBuilder str = new StringBuilder();

				str.Append("SELECT *");
				str.Append(" FROM [dbo].[tbl_images]");
				str.Append(" WHERE LEFT(image_org, 36) = '" + id + "'");

				DataSet data = base.DBBaseClass.ExecuteQuery(str.ToString());

				if(data.Tables[0].Rows.Count > 0) {
					DataTableReader reader = data.CreateDataReader();
					reader.Read();

					return this.FillImageFromReader(reader);
				}
				else {
					return new ImageItem();
				}
			}

			/// <summary>
			/// Get a image item
			/// </summary>
			/// <param name="pkid">The pkid of a image as string.</param>
			/// <exception cref="ArgumentNullException">If pkid is null.</exception>
			/// <returns>A image</returns>
			public ImageItem GetImageByUniqueID(string pkid) {
				if(pkid == null)
					throw new ArgumentNullException("pkid");

				StringBuilder str = new StringBuilder();

				str.Append("SELECT *");
				str.Append(" FROM [dbo].[tbl_images]");
				str.Append(" WHERE pkid = '" + pkid + "'");

				DataSet data = base.DBBaseClass.ExecuteQuery(str.ToString());

				if(data.Tables[0].Rows.Count > 0) {
					DataTableReader reader = data.CreateDataReader();
					reader.Read();

					return this.FillImageFromReader(reader);
				}
				else {
					return new ImageItem();
				}
			}

			/// <summary>
			/// Get a image item
			/// </summary>
			/// <param name="SmallImageID">The integer ID of a image as string.</param>
			/// <exception cref="ArgumentNullException">If ImageID is null.</exception>
			/// <returns>A image</returns>
			public ImageItem GetImage(string SmallImageID) {
				if(SmallImageID == null)
					throw new ArgumentNullException("SmallImageID");

				StringBuilder str = new StringBuilder();

				str.Append("SELECT *");
				str.Append(" FROM [dbo].[tbl_images]");
				str.Append(" WHERE id = '" + Convert.ToInt64(SmallImageID) + "'");

				DataSet data = base.DBBaseClass.ExecuteQuery(str.ToString());

				if(data.Tables[0].Rows.Count > 0) {
					DataTableReader reader = data.CreateDataReader();
					reader.Read();

					return this.FillImageFromReader(reader);
				}
				else {
					return new ImageItem();
				}
			}

			/// <summary>
			/// Get a image item
			/// </summary>
			/// <param name="SmallImageID">The integer ID of a image as long.</param>
			/// <returns>A image</returns>
			public ImageItem GetImage(long SmallImageID) {
				StringBuilder str = new StringBuilder();

				str.Append("SELECT *");
				str.Append(" FROM [dbo].[tbl_images]");
				str.Append(" WHERE id = '" + SmallImageID + "'");

				DataSet data = base.DBBaseClass.ExecuteQuery(str.ToString());

				if(data.Tables[0].Rows.Count > 0) {
					DataTableReader reader = data.CreateDataReader();
					reader.Read();

					return this.FillImageFromReader(reader);
				}
				else {
					return new ImageItem();
				}
			}

			/// <summary>
			/// Get a set item
			/// </summary>
			/// <param name="SmallImageID">The integer ID of a set as string.</param>
			/// <exception cref="ArgumentNullException">If id is null.</exception>
			/// <returns>A set</returns>
			public SetItem GetSet(string id) {
				if(id == null)
					throw new ArgumentNullException("id");

				StringBuilder str = new StringBuilder();

				str.Append("SELECT *");
				str.Append(" FROM [dbo].[tbl_sets]");
				str.Append(" WHERE id = '" + Convert.ToInt64(id) + "'");

				DataSet data = base.DBBaseClass.ExecuteQuery(str.ToString());

				if(data.Tables[0].Rows.Count > 0) {
					DataTableReader reader = data.CreateDataReader();
					reader.Read();

					return this.FillSetFromReader(reader, false);
				}
				else {
					return new SetItem();
				}
			}

			/// <summary>
			/// Get a set item
			/// </summary>
			/// <param name="id">The integer ID of a set as long.</param>
			/// <returns>A set</returns>
			public SetItem GetSet(long id) {
				StringBuilder str = new StringBuilder();

				str.Append("SELECT *");
				str.Append(" FROM [dbo].[tbl_sets]");
				str.Append(" WHERE id = '" + id + "'");

				DataSet data = base.DBBaseClass.ExecuteQuery(str.ToString());

				if(data.Tables[0].Rows.Count > 0) {
					DataTableReader reader = data.CreateDataReader();
					reader.Read();

					return this.FillSetFromReader(reader, false);
				}
				else {
					return new SetItem();
				}
			}

			/// <summary>
			/// Get a set item
			/// </summary>
			/// <param name="SmallImageID">The integer ID of a set as string.</param>
			/// <param name="withimages">If you want to get a set with the images inside, type true</param>
			/// <exception cref="ArgumentNullException">If id is null.</exception>
			/// <returns>A set</returns>
			public SetItem GetSet(string id, bool withimages) {
				if(id == null)
					throw new ArgumentNullException("id");

				StringBuilder str = new StringBuilder();

				str.Append("SELECT *");
				str.Append(" FROM [dbo].[tbl_sets]");
				str.Append(" WHERE id = '" + Convert.ToInt64(id) + "'");

				DataSet data = base.DBBaseClass.ExecuteQuery(str.ToString());

				if(data.Tables[0].Rows.Count > 0) {
					DataTableReader reader = data.CreateDataReader();
					reader.Read();

					return this.FillSetFromReader(reader, withimages);
				}
				else {
					return new SetItem();
				}
			}

			/// <summary>
			/// Get a set item
			/// </summary>
			/// <param name="id">The integer ID of a set as long.</param>
			/// <param name="withimages">If you want to get a set with the images inside, type true</param>
			/// <returns>A set</returns>
			public SetItem GetSet(long id, bool withimages) {
				StringBuilder str = new StringBuilder();

				str.Append("SELECT *");
				str.Append(" FROM [dbo].[tbl_sets]");
				str.Append(" WHERE id = '" + id + "'");

				DataSet data = base.DBBaseClass.ExecuteQuery(str.ToString());

				if(data.Tables[0].Rows.Count > 0) {
					DataTableReader reader = data.CreateDataReader();
					reader.Read();

					return this.FillSetFromReader(reader, withimages);
				}
				else {
					return new SetItem();
				}
			}

			/// <summary>
			/// Get a set item
			/// </summary>
			/// <param name="pkid">The pkid of a set as string.</param>
			/// <exception cref="ArgumentNullException">If pkid is null.</exception>
			/// <returns>A set</returns>
			public SetItem GetSetByUniqueID(string pkid) {
				if(pkid == null) {
					throw new ArgumentNullException("pkid");
				}

				StringBuilder str = new StringBuilder();

				str.Append("SELECT *");
				str.Append(" FROM [dbo].[tbl_sets]");
				str.Append(" WHERE pkid = '" + pkid + "'");

				DataSet data = base.DBBaseClass.ExecuteQuery(str.ToString());

				if(data.Tables[0].Rows.Count > 0) {
					DataTableReader reader = data.CreateDataReader();
					reader.Read();

					return this.FillSetFromReader(reader, false);
				}
				else {
					return new SetItem();
				}
			}

			/// <summary>
			/// Get a set item
			/// </summary>
			/// <param name="pkid">The pkid of a set as string.</param>
			/// <exception cref="ArgumentNullException">If pkid is null.</exception>
			/// <returns>A set</returns>
			public BadgeSettings GetBadgeSettings(string userid) {
				if(userid == null) {
					throw new ArgumentNullException("userid");
				}

				StringBuilder str = new StringBuilder();

				str.Append("SELECT *");
				str.Append(" FROM [dbo].[tbl_badge_settings]");
				str.Append(" WHERE user_pkid = '" + userid + "'");

				DataSet data = base.DBBaseClass.ExecuteQuery(str.ToString());

				if(data.Tables[0].Rows.Count > 0) {
					DataTableReader reader = data.CreateDataReader();
					reader.Read();

					return this.FillBadgeSettingsFromReader(reader);
				}
				else {
					BadgeSettings bs = new BadgeSettings();
					bs.UserID = userid;
					bs.BadgeBorderColor = "ffffff";
					bs.BadgeBorderStyle = "solid";
					bs.BadgeBorderWidth = 0;
					bs.BadgeVertical = true;
					bs.CodeID = MSSystem.Guid.NewGuid().ToString();
					//bs.ID = userid;
					bs.ImageAmount = 3;
					bs.ImageBorderColor = "222222";
					bs.ImageBorderStyle = "solid";
					bs.ImageBorderWidth = 0;
					bs.LinkColor = "222222";
					bs.ZapprLinkPosition = 1;

					return bs;
				}
			}

			/// <summary>
			/// Get a list of all favorite images
			/// </summary>
			/// <param name="start">A amount of items (if end 0 = get all).</param>
			/// <param name="end">end</param>
			/// <param name="valuesperpage"></param>
			/// <param name="withuser"></param>
			/// <returns>A list of all images</returns>
			public List<ImageItem> GetFavoritesList(string userid, int start, int end, int valuesperpage, bool withuser) {
				List<ImageItem> arr = new List<ImageItem>();
				StringBuilder str = new StringBuilder();

				long allimg = this.GetImageAmount();

				if(end != 0) {
					if(valuesperpage > 0) {
						if(end > allimg) {
							start = Convert.ToInt32(allimg % valuesperpage);
						}
					}
				}

				if(end == 0) {
					str.Append("SELECT DISTINCT i.* ");
				}
				else {
					str.Append("SELECT * FROM ( SELECT TOP " + (start == 0 ? end : start) + " * FROM ( ");
					str.Append("SELECT DISTINCT TOP " + end + " i.* ");
				}

				//str.Append("SELECT i.* ");
				str.Append("FROM tbl_favorites AS f ");
				str.Append("INNER JOIN tbl_images AS i ");
				str.Append("ON i.pkid = f.image_pkid ");
				str.Append("WHERE f.user_pkid = '" + userid + "' ");
				str.Append("AND i.access = 0 ");
				str.Append("ORDER BY i.upload_date DESC ");

				if(end != 0) {
					str.Append(" ) AS tmp ORDER BY upload_date ASC ) AS result ORDER BY upload_date DESC");
				}

				DataSet data = base.DBBaseClass.ExecuteQuery(str.ToString());

				if(data.Tables[0].Rows.Count > 0) {
					DataTableReader reader = data.CreateDataReader();

					while(reader.Read()) {
						arr.Add(this.FillImageFromReader(reader, withuser));
					}
				}

				return arr;
			}

			/// <summary>
			/// Get a list of all images
			/// </summary>
			/// <param name="start">A amount of items (if end 0 = get all).</param>
			/// <param name="end">end</param>
			/// <param name="valuesperpage"></param>
			/// <param name="withuser"></param>
			/// <returns>A list of all images</returns>
			public List<ImageItem> GetImageList(int start, int end, int valuesperpage, bool withuser) {
				List<ImageItem> arr = new List<ImageItem>();
				StringBuilder str = new StringBuilder();

				long allimg = this.GetImageAmount();

				if(end != 0) {
					if(valuesperpage > 0) {
						if(end > allimg) {
							start = Convert.ToInt32(allimg % valuesperpage);
						}
						else {
							start = valuesperpage;
						}
					}
				}

				if(end == 0) {
					str.Append("SELECT DISTINCT img.*");
				}
				else {
					str.Append("SELECT * FROM ( SELECT TOP " + (start == 0 ? end : start) + " * FROM (");
					str.Append("SELECT DISTINCT TOP " + end + " img.*");
				}

				str.Append(" FROM [dbo].[tbl_images] AS img");
				str.Append(" WHERE img.access = 0");
				str.Append(" ORDER BY img.upload_date DESC");

				if(end != 0) {
					str.Append(" ) AS tmp ORDER BY upload_date ASC ) AS result ORDER BY upload_date DESC");
				}

				DataSet data = base.DBBaseClass.ExecuteQuery(str.ToString());

				if(data.Tables[0].Rows.Count > 0) {
					DataTableReader reader = data.CreateDataReader();

					while(reader.Read()) {
						arr.Add(this.FillImageFromReader(reader, withuser));
					}
				}

				return arr;
			}

			/// <summary>
			/// Get a list of all images related to a user.
			/// </summary>
			/// <param name="UserID">The ID of a user.</param>
			/// <param name="start">A amount of items (if end 0 = get all).</param>
			/// <param name="end">end</param>
			/// <exception cref="ArgumentNullException">If UserID is null.</exception>
			/// <returns>A ArrayList of all images</returns>
			public ArrayList GetImageList(string UserID, int start, int end, int valuesperpage) {
				if(UserID == null) {
					throw new ArgumentNullException("UserID");
				}

				ArrayList arr = new ArrayList();
				StringBuilder str = new StringBuilder();

				long allimg = this.GetImageAmount(UserID);

				if(end != 0) {
					if(valuesperpage > 0) {
						if(end > allimg) {
							start = Convert.ToInt32(allimg % valuesperpage);
						}
						else {
							start = valuesperpage;
						}
					}
				}

				if(end == 0) {
					str.Append("SELECT DISTINCT img.*");
				}
				else {
					str.Append("SELECT * FROM ( SELECT TOP " + (start == 0 ? end : start) + " * FROM (");
					str.Append("SELECT DISTINCT TOP " + end + " img.*");
				}

				str.Append(" FROM [dbo].[tbl_images] AS img");

				if(UserID == "") {
					str.Append(" WHERE img.access = 0");
				}
				else {
					str.Append(" WHERE (NOT img.access IS NULL) ");
				}

				str.Append(" AND user_pkid = '" + UserID + "' ");
				str.Append(" ORDER BY img.upload_date DESC");

				if(end != 0) {
					str.Append(" ) AS tmp ORDER BY upload_date ASC ) AS result ORDER BY upload_date DESC");
				}

				DataSet data = base.DBBaseClass.ExecuteQuery(str.ToString());

				if(data.Tables[0].Rows.Count > 0) {
					DataTableReader reader = data.CreateDataReader();

					while(reader.Read())
						arr.Add(this.FillImageFromReader(reader));
				}

				return arr;
			}

			/// <summary>
			/// Get a list of all images related to a user.
			/// </summary>
			/// <param name="UserID">The ID of a user.</param>
			/// <param name="start">A amount of items (if end 0 = get all).</param>
			/// <param name="end">end</param>
			/// <param name="valuesperpage"></param>
			/// <param name="aslist"></param>
			/// <exception cref="ArgumentNullException">If UserID is null.</exception>
			/// <returns>A list of all images</returns>
			public List<ImageItem> GetImageList(string UserID, int start, int end, int valuesperpage, bool aslist, bool isLoggedIn) {
				if(UserID == null) {
					throw new ArgumentNullException("UserID");
				}

				List<ImageItem> list = new List<ImageItem>();
				StringBuilder str = new StringBuilder();

				long allimg = this.GetImageAmount(UserID, isLoggedIn);

				if(end != 0) {
					if(valuesperpage > 0) {
						if(end > allimg) {
							start = Convert.ToInt32(allimg % valuesperpage);
						}
						else {
							start = valuesperpage;
						}
					}
				}

				if(end == 0) {
					str.Append("SELECT DISTINCT img.*");
				}
				else {
					str.Append("SELECT * FROM ( SELECT TOP " + (start == 0 ? end : start) + " * FROM (");
					str.Append("SELECT DISTINCT TOP " + end + " img.*");
				}

				str.Append(" FROM [dbo].[tbl_images] AS img");

				if(UserID == "") {
					str.Append(" WHERE img.access = 0");
				}
				else {
					if(isLoggedIn) {
						str.Append(" WHERE (NOT img.access IS NULL) ");
					}
					else {
						str.Append(" WHERE img.access = 0 ");
					}
				}

				str.Append(" AND user_pkid = '" + UserID + "' ");
				str.Append(" ORDER BY img.upload_date DESC");

				if(end != 0) {
					str.Append(" ) AS tmp ORDER BY upload_date ASC ) AS result ORDER BY upload_date DESC");
				}

				DataSet data = base.DBBaseClass.ExecuteQuery(str.ToString());

				if(data.Tables[0].Rows.Count > 0) {
					DataTableReader reader = data.CreateDataReader();

					while(reader.Read()) {
						list.Add(this.FillImageFromReader(reader));
					}
				}

				return list;
			}

			/// <summary>
			/// Get a list of all images related to a user.
			/// </summary>
			/// <param name="userID">The ID of a user.</param>
			/// <param name="currentUser">The current user.</param>
			/// <param name="start">A amount of items (if end 0 = get all).</param>
			/// <param name="end">end</param>
			/// <param name="valuesperpage"></param>
			/// <param name="aslist"></param>
			/// <exception cref="ArgumentNullException">If UserID is null.</exception>
			/// <returns>A list of all images</returns>
			public List<ImageItem> GetImageListForImageBar(string userID, Account currentUser, int start, int end, int valuesperpage) {
				if(userID == null) {
					throw new ArgumentNullException("userID");
				}

				List<ImageItem> list = new List<ImageItem>();
				StringBuilder str = new StringBuilder();

				long allimg = this.GetImageAmount(userID);

				if(end != 0) {
					if(valuesperpage > 0) {
						if(end > allimg) {
							start = Convert.ToInt32(allimg % valuesperpage);
						}
						else {
							start = valuesperpage;
						}
					}
				}

				if(end == 0) {
					str.Append("SELECT DISTINCT img.*");
				}
				else {
					str.Append("SELECT * FROM ( SELECT TOP " + (start == 0 ? end : start) + " * FROM (");
					str.Append("SELECT DISTINCT TOP " + end + " img.*");
				}

				str.Append(" FROM [dbo].[tbl_images] AS img");

				if(currentUser.Username == "" || currentUser.ID != userID) {
					str.Append(" WHERE img.access = 0");
				}
				else {
					str.Append(" WHERE (NOT img.access IS NULL) ");
				}

				str.Append(" AND user_pkid = '" + userID + "' ");
				str.Append(" ORDER BY img.upload_date DESC");

				if(end != 0) {
					str.Append(" ) AS tmp ORDER BY upload_date ASC ) AS result ORDER BY upload_date DESC");
				}

				DataSet data = base.DBBaseClass.ExecuteQuery(str.ToString());

				if(data.Tables[0].Rows.Count > 0) {
					DataTableReader reader = data.CreateDataReader();

					while(reader.Read()) {
						list.Add(this.FillImageFromReader(reader));
					}
				}

				return list;
			}

			/// <summary>
			/// Get a list of all images related to a user.
			/// </summary>
			/// <param name="UserID">The ID of a user.</param>
			/// <param name="start">A amount of items (if end 0 = get all).</param>
			/// <param name="end">end</param>
			/// <param name="valuesperpage"></param>
			/// <param name="aslist"></param>
			/// <exception cref="ArgumentNullException">If UserID is null.</exception>
			/// <returns>A list of all images</returns>
			public List<ImageItem> GetImageListForBadge(string UserID, int start, int end) {
				if(UserID == null) {
					throw new ArgumentNullException("UserID");
				}

				List<ImageItem> list = new List<ImageItem>();
				StringBuilder str = new StringBuilder();

				long allimg = this.GetImageAmount(UserID);

				if(end == 0) {
					str.Append("SELECT DISTINCT img.*");
				}
				else {
					str.Append("SELECT * FROM ( SELECT TOP " + (start == 0 ? end : start) + " * FROM (");
					str.Append("SELECT DISTINCT TOP " + end + " img.*");
				}

				str.Append(" FROM [dbo].[tbl_images] AS img");
				str.Append(" WHERE img.access = 0");
				str.Append(" AND user_pkid = '" + UserID + "' ");
				str.Append(" ORDER BY img.upload_date DESC");

				if(end != 0) {
					str.Append(" ) AS tmp ORDER BY upload_date ASC ) AS result ORDER BY upload_date DESC");
				}

				DataSet data = base.DBBaseClass.ExecuteQuery(str.ToString());

				if(data.Tables[0].Rows.Count > 0) {
					DataTableReader reader = data.CreateDataReader();

					while(reader.Read()) {
						list.Add(this.FillImageFromReader(reader));
					}
				}

				return list;
			}

			/// <summary>
			/// Get a list of all tags related to a image.
			/// </summary>
			/// <param name="ID">The ID of a image.</param>
			/// <exception cref="ArgumentNullException">If ID is null.</exception>
			/// <returns>A ArrayList of all tags</returns>
			public ArrayList GetImageTagList(string ID) {
				if(ID == null)
					throw new ArgumentNullException("ID");

				ArrayList arr = new ArrayList();
				StringBuilder str = new StringBuilder();

				str.Append("SELECT tag.pkid, tag.tag");
				str.Append(" FROM tbl_images_to_tags AS totag");
				str.Append(" INNER JOIN tbl_tags AS tag");
				str.Append(" ON totag.tag_pkid = tag.pkid");
				str.Append(" WHERE totag.image_pkid = '" + ID + "'");
				str.Append(" ORDER BY tag.tag ASC");

				DataSet data = base.DBBaseClass.ExecuteQuery(str.ToString());

				if(data.Tables[0].Rows.Count > 0) {
					DataTableReader reader = data.CreateDataReader();

					while(reader.Read())
						arr.Add(this.FillImageTagFromReader(reader));
				}

				return arr;
			}

			/// <summary>
			/// Get a list of all comments related to a image.
			/// </summary>
			/// <param name="ID">The ID of a image.</param>
			/// <exception cref="ArgumentNullException">If ID is null.</exception>
			/// <returns>A ArrayList of all comments</returns>
			public ArrayList GetImageCommentList(string ID) {
				if(ID == null)
					throw new ArgumentNullException("ID");

				ArrayList arr = new ArrayList();
				StringBuilder str = new StringBuilder();

				str.Append("SELECT *");
				str.Append(" FROM tbl_comments AS cmt");
				str.Append(" INNER JOIN [dbo].[tbl_user] AS usr");
				str.Append(" ON cmt.user_pkid = usr.pkid");
				str.Append(" INNER JOIN [dbo].[tbl_usergroup] AS grp");
				str.Append(" ON usr.group_pkid = grp.pkid");
				str.Append(" WHERE image_pkid = '" + ID + "'");
				str.Append(" ORDER BY date ASC");

				DataSet data = base.DBBaseClass.ExecuteQuery(str.ToString());

				if(data.Tables[0].Rows.Count > 0) {
					DataTableReader reader = data.CreateDataReader();

					while(reader.Read())
						arr.Add(this.FillImageCommentFromReader(reader));
				}

				return arr;
			}

			/// <summary>
			/// Save a set item
			/// </summary>
			/// <param name="set">The set item</param>
			/// <exception cref="ArgumentNullException">If set is null.</exception>
			public void SaveSet(SetItem set) {
				SetItem s = this.SaveSet(set, false);
			}

			/// <summary>
			/// Save a set item
			/// </summary>
			/// <param name="set">The set item</param>
			/// <param name="getback"></param>
			/// <exception cref="ArgumentNullException">If set is null.</exception>
			public SetItem SaveSet(SetItem set, bool getback) {
				if(set == null)
					throw new ArgumentNullException("set");

				if(set.ID == null || set.ID.Trim() == "") {
					set.ID = Guid.NewGuid().ToString();
				}

				StringBuilder str = new StringBuilder();

				str.Append("SELECT *");
				str.Append(" FROM [dbo].[tbl_sets]");
				str.Append(" WHERE pkid = '" + set.ID + "'");

				DataSet dsData = base.DBBaseClass.ExecuteQuery(str.ToString());
				DataRow row = null;

				if(dsData.Tables[0].Rows.Count == 0)
					row = dsData.Tables[0].NewRow();
				else
					row = dsData.Tables[0].Rows[0];

				row["pkid"] = set.ID;
				row["user_pkid"] = set.UserID;
				row["thumbnail"] = set.Thumbnail;
				row["name"] = set.Name;
				row["description"] = set.Description;
				row["access"] = set.Access;

				if(set.CreateDate != null
				&& set.CreateDate.ToString() != "01.01.0001 00:00:00") {
					row["create_date"] = set.CreateDate;
				}

				if(dsData.Tables[0].Rows.Count == 0)
					dsData.Tables[0].Rows.Add(row);

				int i = base.DBBaseClass.ExecuteUpdate(dsData, "tbl_sets");

				if(getback) {
					return this.GetSetByUniqueID(set.ID);
				}
				else {
					return new SetItem();;
				}
			}

			/// <summary>
			/// Save a image item
			/// </summary>
			/// <param name="item">The image item</param>
			/// <exception cref="ArgumentNullException">If item is null.</exception>
			public void SaveImage(ImageItem item) {
				if(item == null) {
					throw new ArgumentNullException("item");
				}

				StringBuilder str = new StringBuilder();

				str.Append("SELECT *");
				str.Append(" FROM [dbo].[tbl_images]");
				str.Append(" WHERE pkid = '" + item.ID + "'");

				DataSet dsData = base.DBBaseClass.ExecuteQuery(str.ToString());
				DataRow row = null;

				if(dsData.Tables[0].Rows.Count == 0) {
					row = dsData.Tables[0].NewRow();
				}
				else {
					row = dsData.Tables[0].Rows[0];
				}

				row["pkid"] = item.ID;
				row["name"] = item.Name;
				row["description"] = item.Description;
				row["user_pkid"] = item.UserID;
				row["image_org"] = item.ImageOriginal;
				row["image_1024"] = item.Image1024;
				row["image_500"] = item.Image500;
				row["image_240"] = item.Image240;
				row["image_100"] = item.Image100;
				row["image_75x75"] = item.Image75x75;
				row["access"] = item.Access;
				row["upload_date"] = item.UploadDate;
				row["type"] = item.Type;
				row["equipment_maker"] = item.EquipmentMaker + "";
				row["equipment_model"] = item.EquipmentModel + "";

				if(item.Licence != null) {
					row["licence"] = item.Licence;
				}

				str.Remove(0, str.Length);
				str.Append("DELETE FROM [dbo].[tbl_images_to_tags]");
				str.Append(" WHERE image_pkid = '" + item.ID + "'");

				int i = base.DBBaseClass.ExecuteNonQuery(str.ToString());

				foreach(TagItem tag in item.Tags) {
					this.SaveTag(tag, item.ID);
				}

				if(dsData.Tables[0].Rows.Count == 0) {
					dsData.Tables[0].Rows.Add(row);
				}

				i = base.DBBaseClass.ExecuteUpdate(dsData, "tbl_images");
			}

			/// <summary>
			/// Save a comment item
			/// </summary>
			/// <param name="item">The comment item</param>
			/// <exception cref="ArgumentNullException">If commentitem is null.</exception>
			public void SaveComment(CommentItem item) {
				if(item == null)
					throw new ArgumentNullException("item");

				StringBuilder str = new StringBuilder();

				str.Append("SELECT *");
				str.Append(" FROM [dbo].[tbl_comments]");
				str.Append(" WHERE pkid = '" + item.ID + "'");

				DataSet dsData = base.DBBaseClass.ExecuteQuery(str.ToString());
				DataRow row = null;

				if(dsData.Tables[0].Rows.Count == 0)
					row = dsData.Tables[0].NewRow();
				else
					row = dsData.Tables[0].Rows[0];

				row["pkid"] = item.ID;
				row["comment"] = item.Comment;
				row["date"] = item.Date;
				row["image_pkid"] = item.ImageID;
				row["user_pkid"] = item.Author.ID;
				
				if(dsData.Tables[0].Rows.Count == 0)
					dsData.Tables[0].Rows.Add(row);

				int i = base.DBBaseClass.ExecuteUpdate(dsData, "tbl_comments");
			}

			/// <summary>
			/// Save a tag item
			/// </summary>
			/// <param name="item">The tag item</param>
			/// <exception cref="ArgumentNullException">If tagitem is null.</exception>
			public void SaveTag(TagItem item, string ImageID) {
				if(item == null) {
					throw new ArgumentNullException("item");
				}

				StringBuilder str = new StringBuilder();

				// the tag
				str.Append("SELECT *");
				str.Append(" FROM [dbo].[tbl_tags]");
				str.Append(" WHERE tag = '" + item.Tag + "'");

				DataSet dsData = base.DBBaseClass.ExecuteQuery(str.ToString());
				DataRow row = null;
				int i;

				if(dsData.Tables[0].Rows.Count == 0) {
					row = dsData.Tables[0].NewRow();

					row["pkid"] = item.ID;
					row["tag"] = item.Tag;

					dsData.Tables[0].Rows.Add(row);

					i = base.DBBaseClass.ExecuteUpdate(dsData, "tbl_tags");
				}
				else {
					DataTableReader reader = dsData.CreateDataReader();
					reader.Read();

					item.ID = reader["pkid"].ToString();
				}

				// the relation to the image
				if(ImageID != "") {
					string pkid = Guid.NewGuid().ToString();
					
					i = base.DBBaseClass.ExecuteNonQuery(str.ToString());

					str.Remove(0, str.Length);
					str.Append("SELECT *");
					str.Append(" FROM [dbo].[tbl_images_to_tags]");
					str.Append(" WHERE tag_pkid = '" + item.ID + "'");
					str.Append(" AND image_pkid = '" + ImageID + "'");

					DataSet dsData2 = base.DBBaseClass.ExecuteQuery(str.ToString());
					DataRow row2 = null;

					if(dsData2.Tables[0].Rows.Count == 0) {
						row2 = dsData2.Tables[0].NewRow();
					}
					else {
						row2 = dsData2.Tables[0].Rows[0];
					}

					row2["pkid"] = Guid.NewGuid().ToString();
					row2["image_pkid"] = ImageID;
					row2["tag_pkid"] = item.ID;

					if(dsData2.Tables[0].Rows.Count == 0) {
						dsData2.Tables[0].Rows.Add(row2);
					}

					i = base.DBBaseClass.ExecuteUpdate(dsData2, "tbl_images_to_tags");
				}
			}

			/// <summary>
			/// Save a favorite image
			/// </summary>
			/// <param name="userid"></param>
			/// <param name="imageid"></param>
			/// <exception cref="ArgumentNullException">If userid or imageid is null.</exception>
			public void SaveFavoriteImage(string userid, string imageid) {
				if(userid == null) {
					throw new ArgumentNullException("userid");
				}

				if(imageid == null) {
					throw new ArgumentNullException("imageid");
				}

				StringBuilder str = new StringBuilder();

				// the tag
				str.Append("SELECT *");
				str.Append(" FROM [dbo].[tbl_favorites]");
				str.Append(" WHERE image_pkid = '" + imageid + "'");
				str.Append(" AND user_pkid = '" + userid + "'");

				DataSet dsData = base.DBBaseClass.ExecuteQuery(str.ToString());
				DataRow row = null;
				int i;

				row = dsData.Tables[0].NewRow();

				row["pkid"] = DBInfo.Current.NewGUID;
				row["user_pkid"] = userid;
				row["image_pkid"] = imageid;

				dsData.Tables[0].Rows.Add(row);

				i = base.DBBaseClass.ExecuteUpdate(dsData, "tbl_favorites");
			}

			/// <summary>
			/// Delete a set
			/// </summary>
			/// <param name="ImageID">The id of the set.</param>
			/// <exception cref="ArgumentNullException">If id is null.</exception>
			public void DeleteSet(string id) {
				if(id == null)
					throw new ArgumentNullException("id");

				StringBuilder str = new StringBuilder();

				// delete set
				str.Append("DELETE FROM [dbo].[tbl_sets]");
				str.Append(" WHERE pkid = '" + id + "'");

				int iRes = base.DBBaseClass.ExecuteNonQuery(str.ToString());
			}

			/// <summary>
			/// Delete a image from a set
			/// </summary>
			/// <param name="imageid">The id of the image.</param>
			/// <param name="setid">The id of the set.</param>
			/// <exception cref="ArgumentNullException">If imageid or setid is null.</exception>
			public void DeleteImageFromSet(string imageid, string setid) {
				if(imageid == null) {
					throw new ArgumentNullException("imageid");
				}

				if(setid == null) {
					throw new ArgumentNullException("setid");
				}

				StringBuilder str = new StringBuilder();

				// delete set
				str.Append("DELETE FROM [dbo].[tbl_sets_to_images]");
				str.Append(" WHERE image_pkid = '" + imageid + "'");
				str.Append(" AND set_pkid = '" + setid + "'");

				int iRes = base.DBBaseClass.ExecuteNonQuery(str.ToString());
			}

			/// <summary>
			/// Delete a image
			/// </summary>
			/// <param name="ImageID">The id of the image.</param>
			/// <exception cref="ArgumentNullException">If ImageID is null.</exception>
			public void DeleteImage(string ImageID) {
				if(ImageID == null)
					throw new ArgumentNullException("ImageID");
				
				StringBuilder str = new StringBuilder();

				// delete image
				str.Append("DELETE FROM [dbo].[tbl_images]");
				str.Append(" WHERE pkid = '" + ImageID + "'");

				int iRes = base.DBBaseClass.ExecuteNonQuery(str.ToString());

				// delete tag relation
				str.Remove(0, str.Length);
				str.Append("DELETE FROM [dbo].[tbl_images_to_tags]");
				str.Append(" WHERE image_pkid = '" + ImageID + "'");

				iRes = base.DBBaseClass.ExecuteNonQuery(str.ToString());

				// delete comments
				str.Remove(0, str.Length);
				str.Append("DELETE FROM [dbo].[tbl_comments]");
				str.Append(" WHERE image_pkid = '" + ImageID + "'");

				iRes = base.DBBaseClass.ExecuteNonQuery(str.ToString());

				// delete views
				str.Remove(0, str.Length);
				str.Append("DELETE FROM [dbo].[tbl_image_view]");
				str.Append(" WHERE image_pkid = '" + ImageID + "'");

				iRes = base.DBBaseClass.ExecuteNonQuery(str.ToString());
			}

			/// <summary>
			/// Delete a favorite from a image
			/// </summary>
			/// <param name="userid"></param>
			/// <param name="imageid">The id of the image.</param>
			/// <exception cref="ArgumentNullException">If userid or imageid is null.</exception>
			public void DeleteFavorite(string userid, string imageid) {
				if(userid == null) {
					throw new ArgumentNullException("userid");
				}

				if(imageid == null) {
					throw new ArgumentNullException("imageid");
				}

				StringBuilder str = new StringBuilder();

				// delete set
				str.Append("DELETE FROM [dbo].[tbl_favorites]");
				str.Append(" WHERE image_pkid = '" + imageid + "'");
				str.Append(" AND user_pkid = '" + userid + "'");

				int iRes = base.DBBaseClass.ExecuteNonQuery(str.ToString());
			}

			/// <summary>
			/// Fill a imageitem from a tablereader
			/// </summary>
			/// <param name="reader"></param>
			/// <returns></returns>
			public ImageItem FillImageFromReader(DataTableReader reader) {
				return this.FillImageFromReader(reader, false);
			}

			/// <summary>
			/// Fill a imageitem from a tablereader
			/// </summary>
			/// <param name="reader"></param>
			/// <param name="withuser"></param>
			/// <returns></returns>
			public ImageItem FillImageFromReader(DataTableReader reader, bool withuser) {
				ImageItem item = new ImageItem();

				item.ID = reader["pkid"].ToString();
				item.ImageID = reader.GetInt64(reader.GetOrdinal("id"));
				item.UserID = reader["user_pkid"].ToString();
				item.Name = reader["name"].ToString();
				item.Description = reader["description"].ToString();
				item.ImageOriginal = reader["image_org"].ToString();
				item.Image1024 = reader["image_1024"].ToString();
				item.Image500 = reader["image_500"].ToString();
				item.Image240 = reader["image_240"].ToString();
				item.Image100 = reader["image_100"].ToString();
				item.Image75x75 = reader["image_75x75"].ToString();
				item.Access = reader.GetInt16(reader.GetOrdinal("access"));
				item.UploadDate = reader.GetDateTime(reader.GetOrdinal("upload_date"));
				item.Tags = this.GetImageTagList(item.ID);
				item.Type = reader.GetInt16(reader.GetOrdinal("type"));

				if(!reader.IsDBNull(reader.GetOrdinal("licence"))) {
					item.Licence = reader["licence"].ToString();
				}

				if(!reader.IsDBNull(reader.GetOrdinal("equipment_maker"))) {
					item.EquipmentMaker = reader["equipment_maker"].ToString();
				}

				if(!reader.IsDBNull(reader.GetOrdinal("equipment_model"))) {
					item.EquipmentModel = reader["equipment_model"].ToString();
				}

				if(withuser) {
					DBAccount db = new DBAccount();
					Account acc = db.GetAccount(item.UserID, GetAccountType.ByID);
					item.User = acc;
				}

				return item;
			}

			/// <summary>
			/// Fill a imagetagitem from a tablereader
			/// </summary>
			/// <param name="reader"></param>
			/// <returns></returns>
			public TagItem FillImageTagFromReader(DataTableReader reader) {
				return this.FillImageTagFromReader(reader, false);
			}

			/// <summary>
			/// Fill a imagetagitem from a tablereader
			/// </summary>
			/// <param name="reader"></param>
			/// <returns></returns>
			public TagItem FillImageTagFromReader(DataTableReader reader, bool withamount) {
				TagItem item = new TagItem();

				item.ID = reader["pkid"].ToString();
				item.Tag = reader["tag"].ToString();

				if(withamount) {
					item.Amount = reader.GetInt32(reader.GetOrdinal("amount"));
				}

				return item;
			}

			/// <summary>
			/// Fill a badge settings from a tablereader
			/// </summary>
			/// <param name="reader"></param>
			/// <returns></returns>
			public BadgeSettings FillBadgeSettingsFromReader(DataTableReader reader) {
				BadgeSettings bs = new BadgeSettings();

				bs.BadgeID = reader.GetInt32(reader.GetOrdinal("id"));
				bs.ID = reader["pkid"].ToString();
				bs.UserID = reader["user_pkid"].ToString();
				bs.CodeID = reader["code_pkid"].ToString();
				bs.BadgeVertical = reader.GetBoolean(reader.GetOrdinal("badge_vertical"));
				bs.ImageAmount = reader.GetInt32(reader.GetOrdinal("image_amount"));
				bs.LinkColor = reader["link_color"].ToString();
				bs.ImageBorderColor = reader["image_border_color"].ToString();
				bs.ImageBorderWidth = reader.GetInt32(reader.GetOrdinal("image_border_width"));
				bs.ImageBorderStyle = reader["image_border_style"].ToString();
				bs.ZapprLinkPosition = reader.GetInt32(reader.GetOrdinal("zappr_link_position"));
				bs.BadgeBorderColor = reader["badge_border_color"].ToString();
				bs.BadgeBorderWidth = reader.GetInt32(reader.GetOrdinal("badge_border_width"));
				bs.BadgeBorderStyle = reader["badge_border_style"].ToString();

				return bs;
			}

			/// <summary>
			/// Fill a imagecommentitem from a tablereader
			/// </summary>
			/// <param name="reader"></param>
			/// <returns></returns>
			public CommentItem FillImageCommentFromReader(DataTableReader reader) {
				CommentItem item = new CommentItem();

				item.ID = reader["pkid"].ToString();
				item.Comment = reader["comment"].ToString();
				item.Date = reader.GetDateTime(reader.GetOrdinal("date"));
				item.ImageID = reader["image_pkid"].ToString();

				DBAccount db = new DBAccount();
				item.Author = db.FillAccountFromTableReader(reader, 0);

				return item;
			}

			/// <summary>
			/// Fill a set from a tablereader
			/// </summary>
			/// <param name="reader"></param>
			/// <returns></returns>
			public SetItem FillSetFromReader(DataTableReader reader, bool withimages) {
				SetItem set = new SetItem();

				set.ID = reader["pkid"].ToString();
				set.SetID = reader.GetInt64(reader.GetOrdinal("id"));
				set.UserID = reader["user_pkid"].ToString();

				if(!reader.IsDBNull(reader.GetOrdinal("thumbnail"))) {
					set.Thumbnail = reader.GetInt64(reader.GetOrdinal("thumbnail"));
				}
				else {
					set.Thumbnail = -1;
				}
				
				set.Name = reader["name"].ToString();
				set.Description = reader["description"].ToString();
				set.CreateDate = reader.GetDateTime(reader.GetOrdinal("create_date"));

				if(withimages) {
					StringBuilder str = new StringBuilder();

					str.Append("SELECT * ");
					str.Append("FROM [dbo].[tbl_images] AS i ");
					str.Append("INNER JOIN [dbo].[tbl_sets_to_images] AS sti ");
					str.Append("ON sti.image_pkid = i.pkid ");
					str.Append("WHERE sti.set_pkid = '" + reader["pkid"].ToString() + "'");

					DataSet dt = base.DBBaseClass.ExecuteQuery(str.ToString());

					if(dt.Tables[0].Rows.Count > 0) {
						DataTableReader rd = dt.CreateDataReader();

						while(rd.Read()) {
							ImageItem item = this.FillImageFromReader(rd);
							set.Images.Add(item);
						}
					}
				}

				return set;
			}

			/// <summary>
			/// Get a list of all images.
			/// </summary>
			/// <param name="Amount">A amount of items (0 = All).</param>
			/// <returns>A ArrayList of images</returns>
			public ArrayList GetNewestImageList(int Amount) {
				ArrayList arr = new ArrayList();
				StringBuilder str = new StringBuilder();

				if(Amount == 0)
					str.Append("SELECT *");
				else
					str.Append("SELECT TOP " + Amount + " *");

				str.Append(" FROM [dbo].[tbl_images]");
				str.Append(" WHERE access = 0");
				str.Append(" ORDER BY upload_date DESC");

				DataSet data = base.DBBaseClass.ExecuteQuery(str.ToString());

				if(data.Tables[0].Rows.Count > 0) {
					DataTableReader reader = data.CreateDataReader();

					while(reader.Read())
						arr.Add(this.FillImageFromReader(reader));
				}

				return arr;
			}

			/// <summary>
			/// Get a imagelist by tag
			/// </summary>
			/// <param name="tag"></param>
			/// <param name="start"></param>
			/// <param name="end"></param>
			/// <returns></returns>
			public List<ImageItem> GetImageListByTag(string tag, int type, int start, int end) {
				return this.GetImageListByTag(tag, type, start, end, "", 0);
			}

			/// <summary>
			/// Get a imagelist by tag
			/// </summary>
			/// <param name="tag"></param>
			/// <param name="start"></param>
			/// <param name="end"></param>
			/// <param name="userid"></param>
			/// <param name="valuesperpage"></param>
			/// <returns></returns>
			public List<ImageItem> GetImageListByTag(string tag, int type, int start, int end, string userid, int valuesperpage) {
				List<ImageItem> list = new List<ImageItem>();
				StringBuilder str = new StringBuilder();
				long allimg = 0;

				if(userid == "") {
					allimg = this.GetImageAmountByTag(tag, type);
				}
				else {
					allimg = this.GetImageAmountByTag(userid, tag, type);
				}

				if(end != 0) {
					if(end > allimg) {
						if(valuesperpage == 0) {
							start = Convert.ToInt32(allimg % 20);
						}
						else {
							start = Convert.ToInt32(allimg % valuesperpage);
						}
					}
				}

				if(end == 0) {
					str.Append("SELECT DISTINCT img.* ");
				}
				else {
					str.Append("SELECT * FROM ( SELECT TOP " + (start == 0 ? end : start) + " * FROM ( ");
					str.Append("SELECT DISTINCT TOP " + end + " img.* ");
				}

				str.Append("FROM tbl_tags AS tags ");
				str.Append("INNER JOIN tbl_images_to_tags AS itt ");
				str.Append("ON itt.tag_pkid = tags.pkid ");
				str.Append("INNER JOIN tbl_images AS img ");
				str.Append("ON itt.image_pkid = img.pkid ");

				if(userid.Trim() != "") {
					str.Append("INNER JOIN tbl_user AS usr ");
					str.Append("ON img.user_pkid = usr.pkid ");
				}

				str.Append("WHERE tag = '" + tag + "' ");

				if(userid.Trim() != "") {
					str.Append("AND usr.[pkid] = '" + userid + "' ");
				}
				else {
					str.Append(" AND img.access = 0 ");
				}

				if(type > 0) {
					str.Append(" AND (img.[type] = " + type.ToString() + ") ");
				}

				str.Append("ORDER BY img.upload_date DESC ");

				if(end != 0) {
					str.Append(") AS tmp ORDER BY upload_date ASC) AS result ORDER BY upload_date DESC");
				}

				DataSet data = base.DBBaseClass.ExecuteQuery(str.ToString());

				if(data.Tables[0].Rows.Count > 0) {
					DataTableReader reader = data.CreateDataReader();

					while(reader.Read())
						list.Add(this.FillImageFromReader(reader, true));
				}

				return list;
			}

			/// <summary>
			/// Get a imagelist by cam
			/// </summary>
			/// <param name="cam"></param>
			/// <param name="type"></param>
			/// <param name="start"></param>
			/// <param name="end"></param>
			/// <returns></returns>
			public List<ImageItem> GetImageListByCam(string cam, int type, int start, int end, bool isLoggedIn) {
				return this.GetImageListByCam(cam, type, start, end, "", 0, isLoggedIn);
			}

			/// <summary>
			/// Get a imagelist by cam
			/// </summary>
			/// <param name="cam"></param>
			/// <param name="type"></param>
			/// <param name="start"></param>
			/// <param name="end"></param>
			/// <param name="userID"></param>
			/// <param name="valuesPerPage"></param>
			/// <param name="isLoggedIn"></param>
			/// <returns></returns>
			public List<ImageItem> GetImageListByCam(string cam, int type, int start, int end, string userID, int valuesPerPage, bool isLoggedIn) {
				List<ImageItem> list = new List<ImageItem>();
				StringBuilder str = new StringBuilder();
				long allimg = 0;

				if(userID == "") {
					allimg = this.GetImageAmountByCam(cam, type, isLoggedIn);
				}
				else {
					allimg = this.GetImageAmountByCam(userID, cam, type, isLoggedIn);
				}

				if(end != 0) {
					if(end > allimg) {
						if(valuesPerPage == 0) {
							start = Convert.ToInt32(allimg % 20);
						}
						else {
							start = Convert.ToInt32(allimg % valuesPerPage);
						}
					}
					else {
						start = valuesPerPage;
					}
				}

				if(end == 0) {
					str.Append("SELECT * ");
				}
				else {
					str.Append("SELECT * FROM ( SELECT TOP " + (start == 0 ? end : start) + " * FROM ( ");
					str.Append("SELECT TOP " + end + " img.* ");
					//str.Append("usr.group_pkid, ");
					//str.Append("usr.title, ");
					//str.Append("usr.username, ");
					//str.Append("usr.password, ");
					//str.Append("usr.email, ");
					//str.Append("usr.webpage, ");
					//str.Append("usr.webpage_name, ");
					//str.Append("usr.firstname, ");
					//str.Append("usr.lastname, ");
					//str.Append("usr.[desc], ");
					//str.Append("usr.birth, ");
					//str.Append("usr.location, ");
					//str.Append("usr.country, ");
					//str.Append("usr.state, ");
					//str.Append("usr.street, ");
					//str.Append("usr.zipcode, ");
					//str.Append("usr.register_date, ");
					//str.Append("usr.enabled, ");
					//str.Append("usr.pro_start_date, ");
					//str.Append("usr.pro_duration, ");
					//str.Append("usr.avatar, ");
					//str.Append("usr.upgrade_in_work, ");
					//str.Append("usr.language ");
				}

				str.Append(" FROM tbl_images AS img ");
				str.Append(" INNER JOIN tbl_user AS usr ");
				str.Append(" ON img.user_pkid = usr.pkid ");
				str.Append(" WHERE NOT (equipment_model IS NULL) ");
				str.Append(" AND NOT (equipment_maker IS NULL) ");
				str.Append(" AND ( ");
				str.Append(" equipment_maker LIKE '%" + cam + "%' ");
				str.Append(" OR equipment_model LIKE '%" + cam + "%' ");
				str.Append(" OR equipment_maker + ' ' + equipment_model LIKE '%" + cam + "%' ");
				str.Append(" ) ");

				if(isLoggedIn && userID != "") {
					str.Append(" AND NOT (img.access IS NULL) ");
					str.Append(" AND usr.[pkid] = '" + userID + "' ");
				}
				else if(isLoggedIn) {
					str.Append(" AND NOT (img.access IS NULL) ");
				}
				else {
					str.Append(" AND img.access = 0 ");
				}

				if(type > 0) {
					str.Append(" AND (img.[type] = " + type.ToString() + ") ");
				}

				str.Append(" ORDER BY img.upload_date DESC ");

				if(end != 0) {
					str.Append(") AS tmp ORDER BY upload_date ASC) AS result ORDER BY upload_date DESC");
				}

				DataSet data = base.DBBaseClass.ExecuteQuery(str.ToString());

				if(data.Tables[0].Rows.Count > 0) {
					DataTableReader reader = data.CreateDataReader();

					while(reader.Read()) {
						list.Add(this.FillImageFromReader(reader, true));
					}
				}

				return list;
			}

			/// <summary>
			/// get a top ten list for the cameras
			/// </summary>
			/// <returns></returns>
			public List<Camera> GetTopTenCameraList() {
				List<Camera> list = new List<Camera>();
				StringBuilder str = new StringBuilder();

				str.Append("SELECT TOP 10 ");
				str.Append(" equipment_maker, ");
				str.Append(" equipment_model, ");
				str.Append(" COUNT(equipment_model) AS equipment_model_amount ");
				str.Append(" FROM tbl_images ");
				str.Append(" WHERE NOT (equipment_model IS NULL) ");
				str.Append(" AND NOT (equipment_maker IS NULL) ");
				str.Append(" AND NOT (equipment_maker = '') ");
				str.Append(" AND access = 0 ");
				str.Append(" GROUP BY equipment_maker, equipment_model ");
				str.Append(" ORDER BY equipment_model_amount DESC ");

				DataSet data = base.DBBaseClass.ExecuteQuery(str.ToString());

				if(data.Tables[0].Rows.Count > 0) {
					DataTableReader reader = data.CreateDataReader();

					while(reader.Read()) {
						Camera c = new Camera();

						c.Maker = reader["equipment_maker"].ToString();
						c.Model = reader["equipment_model"].ToString();
						c.Amount = Convert.ToInt64(
							reader.GetInt32(reader.GetOrdinal("equipment_model_amount"))
						);

						list.Add(c);
					}
				}

				return list;
			}

			/// <summary>
			/// Get a imagelist by params
			/// </summary>
			/// <param name="word"></param>
			/// <param name="type"></param>
			/// <param name="start"></param>
			/// <param name="end"></param>
			/// <returns></returns>
			public ArrayList GetImageListByParam(string word, int type, int start, int end) {
				return this.GetImageListByParam(word, type, start, end, "");
			}

			/// <summary>
			/// Get a imagelist by params
			/// </summary>
			/// <param name="word"></param>
			/// <param name="type"></param>
			/// <param name="start"></param>
			/// <param name="end"></param>
			/// <param name="userid"></param>
			/// <returns></returns>
			public ArrayList GetImageListByParam(string word, int type, int start, int end, string userid) {
				ArrayList arr = new ArrayList();
				StringBuilder str = new StringBuilder();

				long allimg = 0;

				if(userid == "") {
					allimg = this.GetImageAmount(word, type, true);
				}
				else {
					allimg = this.GetImageAmount(userid, type, word);
				}

				if(end != 0) {
					if(end > allimg) {
						start = Convert.ToInt32(allimg % 20);
					}
				}

				if(end == 0) {
					str.Append("SELECT DISTINCT img.*");
				}
				else {
					str.Append("SELECT * FROM ( SELECT TOP " + (start == 0 ? end : start) + " * FROM (");
					str.Append("SELECT DISTINCT TOP " + end + " img.*");
				}

				str.Append(" FROM [dbo].[tbl_images] AS img");
				str.Append(" INNER JOIN [dbo].[tbl_user] AS usr");
				str.Append(" ON usr.pkid = img.user_pkid");
				str.Append(" WHERE (img.access = 0) ");

				if(userid.Trim() != "") {
					str.Append(" AND (usr.[pkid] = '" + userid + "') ");
				}

				if(type > 0) {
					str.Append(" AND (img.[type] = " + type.ToString() + ") ");
				}

				str.Append(" AND (img.[name] LIKE '%" + word + "%'");
				str.Append(" OR img.description LIKE '%" + word + "%') ");
				str.Append(" ORDER BY img.upload_date DESC");

				if(end != 0) {
					str.Append(" ) AS tmp ORDER BY upload_date ASC ) AS result ORDER BY upload_date DESC");
				}

				DataSet data = base.DBBaseClass.ExecuteQuery(str.ToString());

				if(data.Tables[0].Rows.Count > 0) {
					DataTableReader reader = data.CreateDataReader();

					while(reader.Read())
						arr.Add(this.FillImageFromReader(reader, true));
				}

				return arr;
			}

			/// <summary>
			/// Get a random image item
			/// </summary>
			/// <returns></returns>
			public ImageItem GetRandomImage() {
				StringBuilder str = new StringBuilder();

				str.Append(" SELECT id ");
				str.Append(" FROM [dbo].[tbl_images] ");
				str.Append(" WHERE [access] = 0 ");
				str.Append(" AND [type] = 1 ");

				long random = 0;
				int max = 0;
				DataSet data = base.DBBaseClass.ExecuteQuery(str.ToString());

				if(data.Tables[0].Rows.Count > 0) {
					DataTableReader reader = data.CreateDataReader();
					List<long> list = new List<long>();

					while(reader.Read()) {
						list.Add(reader.GetInt64(reader.GetOrdinal("id")));
					}

					//max = Convert.ToInt32(reader[0].ToString());
					max = list.Count;

					ImageItem item = new ImageItem();
					Random rand = new Random();
					random = Convert.ToInt64(rand.Next(max));

					item = this.GetImage(list[Convert.ToInt32(random)]);

					if(item.ID == null) {
						while(item.ID == null) {
							random = Convert.ToInt64(rand.Next(max));
							item = this.GetImage(list[Convert.ToInt32(random)]);
						}
					}

					return item;
				}
				else {
					return new ImageItem();
				}
			}

			/// <summary>
			/// Get a list of all tags with its popularity
			/// </summary>
			/// <returns></returns>
			public ArrayList GetTagPopularityList() {
				StringBuilder str = new StringBuilder();

				str.Append("SELECT * FROM ( ");
				str.Append("SELECT TOP 100 tags.pkid, tags.tag, COUNT(tags.tag) AS amount ");
				str.Append("FROM [dbo].[tbl_tags] AS tags ");
				str.Append("INNER JOIN [dbo].[tbl_images_to_tags] AS it ");
				str.Append("ON it.tag_pkid = tags.pkid ");
				str.Append("WHERE (NOT tags.tag = '') ");
				str.Append("GROUP BY tags.pkid, tags.tag ");
				str.Append("ORDER BY amount DESC");
				str.Append(") AS list ");
				str.Append("ORDER BY tag ASC");

				DataSet data = base.DBBaseClass.ExecuteQuery(str.ToString());

				if(data.Tables[0].Rows.Count > 0) {
					ArrayList arr = new ArrayList();
					DataTableReader reader = data.CreateDataReader();

					while(reader.Read()) {
						arr.Add(this.FillImageTagFromReader(reader, true));
					}

					return arr;
				}
				else {
					return new ArrayList();
				}
			}

			/// <summary>
			/// Get the amount of sets related to a user.
			/// </summary>
			/// <param name="id"></param>
			/// <returns></returns>
			public int GetUserSetsAmount(string id) {
				StringBuilder str = new StringBuilder();

				str.Append("SELECT COUNT(pkid) AS amount ");
				str.Append("FROM [dbo].[tbl_sets] AS s ");
				str.Append("WHERE s.user_pkid = '" + id + "'");

				DataSet data = base.DBBaseClass.ExecuteQuery(str.ToString());

				if(data.Tables[0].Rows.Count > 0) {
					DataTableReader reader = data.CreateDataReader();
					reader.Read();

					return reader.GetInt32(reader.GetOrdinal("amount"));
				}
				else {
					return 0;
				}
			}

			/// <summary>
			/// Get a list of sets. The parameter "withimages" decide if the images should load.
			/// </summary>
			/// <param name="id"></param>
			/// <param name="withimages"></param>
			/// <returns></returns>
			public List<SetItem> GetUserSets(string id, bool withimages) {
				StringBuilder str = new StringBuilder();

				str.Append("SELECT * ");
				str.Append("FROM [dbo].[tbl_sets] AS s ");
				str.Append("WHERE s.user_pkid = '" + id + "'");

				DataSet data = base.DBBaseClass.ExecuteQuery(str.ToString());

				if(data.Tables[0].Rows.Count > 0) {
					DataTableReader reader = data.CreateDataReader();

					List<SetItem> list = new List<SetItem>();

					while(reader.Read()) {
						list.Add(this.FillSetFromReader(reader, withimages));
					}

					return list;
				}
				else {
					return null;
				}
			}

			/// <summary>
			/// Check if a image is set to a image-set
			/// </summary>
			/// <param name="id">The id of the image</param>
			/// <returns></returns>
			/// <exception cref="ArgumentNullException">If id is null.</exception>
			public bool ImageHasSet(string id) {
				if(id == null) {
					throw new ArgumentNullException(
						"id", 
						"The parameter id cannot be null!"
					);
				}

				StringBuilder str = new StringBuilder();

				str.Append("SELECT * ");
				str.Append("FROM [dbo].[tbl_images] AS i ");
				str.Append("INNER JOIN [dbo].[tbl_sets_to_images] AS sti ");
				str.Append("ON sti.image_pkid = i.pkid ");
				str.Append("WHERE i.pkid = '" + id + "'");

				DataSet data = base.DBBaseClass.ExecuteQuery(str.ToString());

				if(data.Tables[0].Rows.Count == 0) {
					return false;
				}
				else {
					return true;
				}
			}

			/// <summary>
			/// Check if a image is a favorite image of a user
			/// </summary>
			/// <param name="userid"></param>
			/// <param name="imageid"></param>
			/// <returns></returns>
			/// <exception cref="ArgumentNullException">If userid or imageid is null.</exception>
			public bool ImageIsFavorite(string userid, string imageid) {
				if(userid == null) {
					throw new ArgumentNullException("userid");
				}

				if(imageid == null) {
					throw new ArgumentNullException("imageid");
				}

				StringBuilder str = new StringBuilder();

				str.Append("SELECT * ");
				str.Append("FROM tbl_favorites ");
				str.Append("WHERE user_pkid = '" + userid + "' ");
				str.Append("AND image_pkid = '" + imageid + "'");

				DataSet data = base.DBBaseClass.ExecuteQuery(str.ToString());

				if(data.Tables[0].Rows.Count == 0) {
					return false;
				}
				else {
					return true;
				}
			}

			/// <summary>
			/// Add a image to a set
			/// </summary>
			/// <param name="imageid"></param>
			/// <param name="setid"></param>
			public void AddImageToSet(string imageid, string setid) {
				StringBuilder str = new StringBuilder();

				str.Append("SELECT *");
				str.Append(" FROM [dbo].[tbl_sets_to_images]");
				str.Append(" WHERE image_pkid = '" + imageid + "'");
				str.Append(" AND set_pkid = '" + setid + "'");

				DataSet dsData = base.DBBaseClass.ExecuteQuery(str.ToString());
				DataRow row = null;

				if(dsData.Tables[0].Rows.Count == 0) {
					row = dsData.Tables[0].NewRow();

					row["pkid"] = Guid.NewGuid().ToString();
					row["set_pkid"] = setid;
					row["image_pkid"] = imageid;

					dsData.Tables[0].Rows.Add(row);
				}
				else {
					row = dsData.Tables[0].Rows[0];

					row["set_pkid"] = setid;
					row["image_pkid"] = imageid;
				}
				
				int i = base.DBBaseClass.ExecuteUpdate(dsData, "tbl_sets_to_images");
			}

			/// <summary>
			/// Get all sets for an image
			/// </summary>
			/// <param name="imageid"></param>
			/// <returns></returns>
			public List<SetItem> GetSetsByImageID(string imageid) {
				StringBuilder str = new StringBuilder();

				str.Append("SELECT sets.* ");
				str.Append("FROM tbl_sets_to_images AS sti ");
				str.Append("INNER JOIN tbl_sets AS sets ");
				str.Append("ON sti.set_pkid = sets.pkid ");
				str.Append("WHERE image_pkid = '" + imageid + "'");

				DataSet data = base.DBBaseClass.ExecuteQuery(str.ToString());

				if(data.Tables[0].Rows.Count > 0) {
					DataTableReader reader = data.CreateDataReader();

					List<SetItem> list = new List<SetItem>();

					while(reader.Read()) {
						list.Add(this.FillSetFromReader(reader, false));
					}

					return list;
				}
				else {
					return null;
				}
			}

			/// <summary>
			/// Check if a set exists
			/// </summary>
			/// <param name="ImageID">The ID of a set.</param>
			/// <exception cref="ArgumentNullException">If setid is null.</exception>
			/// <returns></returns>
			public bool CheckSetExists(string setid) {
				if(setid == null) {
					throw new ArgumentNullException("setid");
				}

				StringBuilder str = new StringBuilder();

				str.Append("SELECT *");
				str.Append(" FROM [dbo].[tbl_sets]");
				str.Append(" WHERE id = '" + Convert.ToInt64(setid) + "'");

				DataSet data = base.DBBaseClass.ExecuteQuery(str.ToString());

				if(data.Tables[0].Rows.Count > 0) {
					return true;
				}
				else {
					return false;
				}
			}

			/// <summary>
			/// Check if a image exists
			/// </summary>
			/// <param name="ImageID">The ID of a image.</param>
			/// <exception cref="ArgumentNullException">If imageid is null.</exception>
			/// <returns></returns>
			public bool CheckImageExists(string imageid) {
				if(imageid == null) {
					throw new ArgumentNullException("imageid");
				}

				StringBuilder str = new StringBuilder();

				str.Append("SELECT * ");
				str.Append("FROM [dbo].[tbl_images] ");
				str.Append("WHERE id = '" + Convert.ToInt64(imageid) + "'");

				DataSet data = base.DBBaseClass.ExecuteQuery(str.ToString());

				if(data.Tables[0].Rows.Count > 0) {
					return true;
				}
				else {
					return false;
				}
			}

			/// <summary>
			/// Count the amount of views of an image
			/// </summary>
			/// <param name="id">The ID of a image.</param>
			public void CountImageView(string id) {
				int res = 0;
				StringBuilder str = new StringBuilder();

				str.Append("SELECT * ");
				str.Append("FROM [dbo].[tbl_image_view] ");
				str.Append("WHERE image_pkid = '" + id + "'");

				DataSet data = base.DBBaseClass.ExecuteQuery(str.ToString());

				if(data.Tables[0].Rows.Count > 0) {
					DataTableReader reader = data.CreateDataReader();
					reader.Read();

					long amount = reader.GetInt64(reader.GetOrdinal("view"));

					amount++;

					DataRow row = data.Tables[0].Rows[0];
					row["view"] = amount;

					res = base.DBBaseClass.ExecuteUpdate(data, "tbl_image_view");
				}
				else {
					DataRow row = data.Tables[0].NewRow();

					row["image_pkid"] = id;
					row["view"] = 1;

					data.Tables[0].Rows.Add(row);

					res = base.DBBaseClass.ExecuteUpdate(data, "tbl_image_view");
				}
			}

			/// <summary>
			/// Get the amount of the mostly used tag
			/// </summary>
			/// <returns></returns>
			public int GetMostlyUsedTagAmount() {
				StringBuilder str = new StringBuilder();

				str.Append("SELECT TOP 1 tags.pkid, tags.tag, COUNT(tags.tag) AS amount ");
				str.Append("FROM [dbo].[tbl_tags] AS tags ");
				str.Append("INNER JOIN [dbo].[tbl_images_to_tags] AS it ");
				str.Append("ON it.tag_pkid = tags.pkid ");
				str.Append("GROUP BY tags.pkid, tags.tag ");
				str.Append("ORDER BY amount DESC");

				DataSet data = base.DBBaseClass.ExecuteQuery(str.ToString());
				DataTableReader reader = data.CreateDataReader();
				reader.Read();

				return reader.GetInt32(reader.GetOrdinal("amount"));
			}

			/// <summary>
			/// Save the badge settings in the db
			/// </summary>
			/// <param name="bs"></param>
			public void SaveBadgeSettings(BadgeSettings bs) {
				if(bs == null)
					throw new ArgumentNullException("bs");

				if(bs.ID == null || bs.ID.Trim() == "") {
					bs.ID = Guid.NewGuid().ToString();
				}

				StringBuilder str = new StringBuilder();

				str.Append("SELECT *");
				str.Append(" FROM [dbo].[tbl_badge_settings]");
				str.Append(" WHERE pkid = '" + bs.ID + "'");

				DataSet dsData = base.DBBaseClass.ExecuteQuery(str.ToString());
				DataRow row = null;

				if(dsData.Tables[0].Rows.Count == 0)
					row = dsData.Tables[0].NewRow();
				else
					row = dsData.Tables[0].Rows[0];

				row["pkid"] = bs.ID;
				row["user_pkid"] = bs.UserID;
				row["code_pkid"] = bs.CodeID;
				row["badge_vertical"] = bs.BadgeVertical;
				row["image_amount"] = bs.ImageAmount;
				row["link_color"] = bs.LinkColor;
				row["image_border_color"] = bs.ImageBorderColor;
				row["image_border_width"] = bs.ImageBorderWidth;
				row["image_border_style"] = bs.ImageBorderStyle;
				row["zappr_link_position"] = bs.ZapprLinkPosition;
				row["badge_border_color"] = bs.BadgeBorderColor;
				row["badge_border_width"] = bs.BadgeBorderWidth;
				row["badge_border_style"] = bs.BadgeBorderStyle;

				if(dsData.Tables[0].Rows.Count == 0)
					dsData.Tables[0].Rows.Add(row);

				int i = base.DBBaseClass.ExecuteUpdate(dsData, "tbl_badge_settings");
			}
		}
	}
}
